/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.model.shared.attr;

import axescon.xacml.api.XacmlException;
import axescon.xacml.model.policy.AttributeValue;

import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.Array;

/**
 * @author argyn
 *         Date: Oct 31, 2005
 *         Time: 9:38:39 PM
 *         Responsibilities: helper utility to create arrays of attribute values
 */
public class AttributeValueHelper {

    public AttributeValueHelper() {
        this(true);
    }

    public AttributeValueHelper(boolean isSetDefaults) {
//        setClasses(new HashMap<String, XBAttributeValueFactory>());

        setClasses(new HashMap());
        if (isSetDefaults) setDefaults();
    }

    /**
     * @param fs Map<String, XBAttributeValueFactory>
     */
    public AttributeValueHelper(Map fs) {
        setClasses(fs);
    }

    public void setDefaults() {
            getClasses().put(AnyURIAttribute.dataType,
                    AnyURIAttribute.class);
            getClasses().put(Base64BinaryAttribute.dataType,
                    Base64BinaryAttribute.class);
            getClasses().put(BooleanAttribute.dataType,
                    BooleanAttribute.class);
            getClasses().put(DateAttribute.dataType,
                    DateAttribute.class);
            getClasses().put(DateTimeAttribute.dataType, DateTimeAttribute
                    .class);
            getClasses().put(DayTimeDurationAttribute.dataType, DayTimeDurationAttribute
                    .class);
            getClasses().put(DoubleAttribute.dataType, DoubleAttribute
                    .class);
            getClasses().put(HexBinaryAttribute.dataType, HexBinaryAttribute
                    .class);
            getClasses().put(IntegerAttribute.dataType, IntegerAttribute
                    .class);
            getClasses().put(RFC822NameAttribute.dataType, RFC822NameAttribute
                    .class);
            getClasses().put(StringAttribute.dataType, StringAttribute
                    .class);
            getClasses().put(TimeAttribute.dataType, TimeAttribute
                    .class);
            getClasses().put(X500NameAttribute.dataType, X500NameAttribute
                    .class);
            getClasses().put(YearMonthDurationAttribute.dataType, YearMonthDurationAttribute
                    .class);
            getClasses().put(IpAddressAttribute.dataType, IpAddressAttribute
                    .class);
            getClasses().put(DnsNameAttribute.dataType, DnsNameAttribute
                    .class);
    }


    public void addClass(String type, Class c) {
        getClasses().put(type, c);
    }

    /**
     * Map<String, AttributeValue.class>
     */
    private Map classes;


    /**
     * @return Map<String, XBAttributeValueFactory>
     */
    public Map getClasses() {
        return classes;
    }

    /**
     * Map<String, XBAttributeValueFactory>
     *
     * @param classes
     */
    public void setClasses(Map classes) {
        this.classes = classes;
    }

    public Class getClass(String type) throws XacmlException {
        Class ret = (Class) ((Map) getClasses()).get(type);
        if (ret == null)
            throw new XacmlException("Unsupported type: " + type,
                    XacmlException.ERR_CODE_PROCESSING_UNSUPPORTED_TYPE);
        return ret;
    }

    /**
     * makes an empty array of attribute values of given type and size
     * @param type
     * @param length
     * @return
     */
    public AttributeValue[] newArray(String type,int length)throws XacmlException{
        return (AttributeValue[])Array.newInstance( getClass(type),length);

    }

    private static final AttributeValueHelper DEFAULT_ATTRIBUTE_VALUE_HELPER =
        new AttributeValueHelper(true);

    public static AttributeValueHelper getDefault(){return DEFAULT_ATTRIBUTE_VALUE_HELPER;}

}
